<?php
class Model_Car extends Model_Template
{
	
	public function __construct()
	{
		parent::__construct();
		$this->className = 'Model_Table_Car';

	}
	
	///////////////////////////////////////////////////////////////////return all car owned by one profile
	public function getAllCars($targetOwner)
	{
		//Select all cars from owner query
		$sqlAllCarFromOwner = 'SELECT * FROM car WHERE Profile_profile_id= ? ;';
		
		//Prepare statement
		$allCarFromOwner = Controller_Template::$db->prepareEKADBPDO($sqlAllCarFromOwner, $this->className, array('car_id','car_history','make','model','year','profile_id','last_oil_change_date'));
		
		//Execute statement
		$allCarFromOwner->execute(array($targetOwner));
		
		
		return $allCarFromOwner->fetchAll();
	}
	
	/////////////////////////////////////////////////////////////create a car for a profile
	public function createNewCar($newC)
	{
		//Create new car query
		$sqlCreateNewCar = 'INSERT INTO car (car_history,make,model,year,Profile_profile_id,last_oil_change_date) VALUES (?, ?, ?, ? ,?, ?);';
		
		//Prepare statement
		$createNewCar = Controller_Template::$db->prepareEKADBPDO($sqlCreateNewCar, $this->className, array('car_id','car_history','make','model','year','Profile_profile_id','last_oil_change_date'));
		
		//Execute statement
		$createNewCar->execute(array("-----[Created]".date("Y-m-d")."------\r\n".$newC[':car_history'],$newC[':make'],$newC[':model'],$newC[':year'],$newC[':profile_id'],$newC[':last_oil_change_date']));
		
		if($createNewCar->rowCount()==1)
		{
			return Controller_Template::$db->lastInsertId();
		}
	}
	
	
	/////////////////////////////////////////////////////Delete a car from profile
	public function deleteCar($targetCar)
	{
		//Delete car query
		$sqlDeleteCar = 'DELETE FROM car WHERE car_id= ? ;';
		
		//Prepare statement
		$deleteNewCar = Controller_Template::$db->prepareEKADBPDO($sqlDeleteCar, $this->className, array('car_id','car_history','make','model','year','profile_id','last_oil_change_date'));
		
		//Execute statement
		$deleteNewCar->execute(array($targetCar));
		
		return $deleteNewCar->rowCount();
	}
	
	
	//////////////////////////////////////////////////Update a car
	public function updateCar($targetCar)
	{
		//Update car query
		$sqlUpdateCar = 'UPDATE car SET car_history=?,make=?,model=?,year=?,Profile_profile_id=?,last_oil_change_date=? WHERE car_id=?;';
		
		//Prepare statement
		$updateNewCar = Controller_Template::$db->prepareEKADBPDO($sqlUpdateCar, $this->className, array('car_id','car_history','make','model','year','profile_id','last_oil_change_date'));
		
		//Execute statement
		$updateNewCar->execute(array($targetCar[':car_history'],$targetCar[':make'],$targetCar[':model'],$targetCar[':year'],$targetCar[':profile_id'],$targetCar[':last_oil_change_date'],$targetCar[':car_id']));
		
		return $updateNewCar->rowCount();
	}
}